package org.jsrml.core.base.service;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authc.AccountException;
import org.hibernate.Criteria;
import org.jsrml.common.persistence.BaseDao;
import org.jsrml.common.util.JSONUtils;
import org.jsrml.common.util.PropertiesUtil;
import org.jsrml.common.util.PwdUtil;
import org.jsrml.core.base.command.SaveAuthAccountCo;
import org.jsrml.core.base.dto.LoginDomanDTO;
import org.jsrml.core.base.entity.AuthAccount;
import org.jsrml.core.base.qo.AuthAccountQO;
import org.jsrml.core.purchaser.entity.Worker;
import org.jsrml.core.purchaser.exception.PurchaserException;
import org.jsrml.core.purchaser.qo.WorkerQO;
import org.jsrml.core.purchaser.service.WorkerService;
import org.jsrml.core.supplier.entity.Vendor;
import org.jsrml.core.supplier.qo.VendorQO;
import org.jsrml.core.supplier.service.VendorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class AuthAccountService extends BaseDao<AuthAccount, AuthAccountQO> {

	@Autowired
	private WorkerService workerService;

	@Autowired
	private VendorService vendorService;

	public AuthAccount createAuthAccount(SaveAuthAccountCo command) {
		AuthAccount account = new AuthAccount();
		account.create(command);
		save(account);
		return account;
	}

	public AuthAccount modifyAuthAccount(SaveAuthAccountCo command) {
		AuthAccount account = get(command.getAuthAccountId());
		account.modify(command);
		save(account);
		return account;
	}

	/**
	 * 主机方员登陆
	 * 
	 * @param request
	 * @param loginName
	 *            用户名
	 * @param password
	 *            密码
	 * @return
	 * @throws PurchaserException
	 */
	public LoginDomanDTO login(String loginName, String password) throws PurchaserException {
		LoginDomanDTO dto = loginValidata(loginName, password);
		
		if(dto == null){
			return null;
		}
		
		switch (dto.getAccountOwner()) {
		case "worker":
			WorkerQO qo = new WorkerQO();
			qo.setAuthAccountId(dto.getAccountId());
			Worker worker = workerService.queryUnique(qo);
			dto.setId(worker.getId());
			dto.setName(worker.getBaseInfo().getName());
			dto.setPayload(JSONUtils.p(JSONUtils.c(worker), Object.class));
			break;
		case "vendor":
			VendorQO vendorQO = new VendorQO();
			vendorQO.setAuthAccountId(dto.getAccountId());
			Vendor vendor = vendorService.queryUnique(vendorQO);
			dto.setId(vendor.getId());
			dto.setName(vendor.getInfo().getVendorName());
			dto.setPayload(JSONUtils.p(JSONUtils.c(vendor), Object.class));
			break;
		default:
			break;
		}
		return dto;
	}

	/**
	 * 账号登陆校验
	 * 
	 * @param loginName
	 * @param password
	 * @return
	 * @throws PurchaserException
	 */
	private LoginDomanDTO loginValidata(String loginName, String password) throws PurchaserException {

		if (StringUtils.isBlank(loginName)) {
			throw new PurchaserException(PurchaserException.NECESSARY_PARAM_NULL, "用户名不能为空!");
		}

		/**
		 * 如果是超级管理员
		 * 从配置文件中读取
		 */
		if (StringUtils.equals(loginName, "admin")) {
			String pass = PropertiesUtil.getProperiesValue("adminLoginPassword", "system.properties");

			if (!pass.equals(password)) {
				throw new AccountException("密码不正确！");
			} else {
				return new LoginDomanDTO("admin","admin","admin","超级管理员");
			}
		}

		// 根据登录名查询帐号
		AuthAccountQO accountQO = new AuthAccountQO();
		accountQO.setLoginName(loginName);
		AuthAccount account = queryUnique(accountQO);

		if (account == null) {
			throw new PurchaserException(PurchaserException.WORKER_NOT_FOUND, "管理员帐号不存在!");
		}
		if (!StringUtils.equals(PwdUtil.getPwd(password), account.getEncryptPassword())) {
			throw new PurchaserException(PurchaserException.PASSWORD_WRONG, "用户名密码不匹配!");
		}
		return new LoginDomanDTO(account.getId(),account.getLoginName(),account.getDomainLink().getDomainType());
	}

	@Override
	protected Criteria buildCriteria(Criteria criteria, AuthAccountQO qo) {
		return criteria;
	}

	@Override
	protected Class<AuthAccount> getEntityClass() {
		return AuthAccount.class;
	}

}
